<template>
    <div >
<ul id="treedeviceoperation" class="ztree"></ul>
 <input type="hidden" id="device_id" value="">
 <input type="hidden" id="companyid" value="">
  <input type="hidden" id="type_val" value="">
</div>
</template>
<style>
</style>
<script>

var setting = {
    check: {
        enable: true,
        
    },
    view: {
        dblClickExpand: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    
    callback: {
     beforeClick: beforeClick,
    
       onCheck: onCheck,
    }
};

/**
 * 使用循环的方式判断一个元素是否存在于一个数组中
 * @param {Object} arr 数组
 * @param {Object} value 元素值
 */
function isInArray(arr,value){
    for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return true;
        }
    }
    return false;
}
var arr = [];
function beforeClick(treeId, treeNode){
   // console.log(treeNode);
  // var zTree = $.fn.zTree.getZTreeObj("treefunctionalmodule");
    //zTree.checkNode(treeNode, !treeNode.checked, null, true);
  $("#"+treeNode.tId+"_check").click();
    return false;
};

function onCheck(e, treeId, treeNode){
   // var zTree = $.fn.zTree.getZTreeObj("treedeviceoperation"),
    //nodes = zTree.getCheckedNodes(true),

   var zTree = $.fn.zTree.getZTreeObj("treedeviceoperation"),
    che=zTree.checkNode(treeId, true),
    nodes = zTree.getCheckedNodes(true),


    v = "",d="",t="";
    for (var i=0, l=nodes.length; i<l; i++) {

if(isNaN(nodes[i].id)){
   console.log(nodes[i].id);
        d += nodes[i].id + ",";
        t+=nodes[i].type + ",";
        if(!isInArray(arr,nodes[i].companyid)){

           v += nodes[i].companyid + ","
           arr.push(nodes[i].companyid);
        }
       
        

}else{
   
  //var   sizezTree= zTree.getCheckedNodes(nodes[i]);
   

  //console.log(sizezTree.length);

   //v += nodes[i].id + ",";
    // companyid


}

    }
    if(v.length>0){ v=nodes[0].id+","+v;}
    if (v.length > 0 ) v = v.substring(0, v.length-1);


     if (d.length > 0 ) d = d.substring(0, d.length-1);
      if (t.length > 0 ) t = t.substring(0, t.length-1);
      arr=[];
    
     
    //var cityObj = $("#citySel");
    //cityObj.attr("value", v);
    $("#companyid").val(v);
    $("#device_id").val(d); 
    $("#type_val").val(t); return;
    //$("body").bind("mousedown", onBodyDown);
};

$(document).ready(function(){ 
    console.log(sessionStorage.getItem("coid")+"在设备选择这里");
companytree();
});
window.companytree=function(){
       domain.ajaxUrl({
                method: 'POST',
                url: domain.testUrl+"/Threeinjection/DeviceManagement/queryCompanyandDevice",
                data: {"sessionid":sessionStorage.getItem("sid"),"coid":sessionStorage.getItem("coid")},
                datatype:"JSON",
                success: function (response) {   
            //alert(response);
                var obj = eval("("+response+")");

                   // console.log(response);
                  var newTree=$.fn.zTree.init($("#treedeviceoperation"), setting, JSON.parse(obj.companyandDevice));
                }
  });

}
 </script>